Vagrant and Ansible on Windows 10
On Linux this would be easy...
Why?
Because I currently work on a project that will be deployed as client application on Windows and i wanted to know if this works.
How?
The process of installing Vagrant on Windows is simple enough. Download Vagrant from here and I recommend to use VirtualBox. To have a nice shell to operate vagrant under windows, here I would suggest installing Git for Windows.
Installing and running Ansible is a bit tougher. To make it easy, we can just use the new Linux subsystem that comes with the Windows 10 Anniversary update. When you have your bash open you can start the installation with:
$ sudo apt-get install ansible
That should be enough to get started.
First Steps with Vagrant
Now we can start to initialize our first virtual machine, so open up the Git Bash. Navigate to the directory where you want to initialize the virtual machine for example C:\Projects\VM.
$ cd /c/Projects/VM
In the next step we initialize a Trusty image. This will download the image for you. Depending on your internet connection this may take some time.
$ vagrant init ubuntu/trusty64
When this has finished we just need to start the machine with the following command.
$ vagrant up
To verify it is running you can use Vagrant to directly ssh to the machine.
$ vagrant ssh
Lets get started with Ansible
For this step we need to switch to Bash for Windows. We can use the same directory we are using with Vagrant, but is is mapped a bit differently than with the MINGW environment that the Git Bash provides.
$ cd /mnt/c/Projects/VM
Ansible works with inventory files that contain all available hosts. So we create one with our virtual machine created by Vagrant.
[nodes]
127.0.0.1 ansible_user=vagrant ansible_port=2222 ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key
So, what do we do here? We tell Ansible that in the group nodes, we have one machine with the ip address 127.0.0.1 and the port 2222. To open a ssh connection, Ansible should use the user name vagrant and the private key that Vagrant provides. We can test if the connection is working by executing the following command.
ansible -i ./hosts all -m shell -a 'touch test'
This will create a file called test in the home folder of the user vagrant. Now we are all set and can get started with playbooks and Ansible Galaxy.
Awesome!